<?php

namespace App\Http\Middleware;

use App\Service\Sms;
use Closure;
use Illuminate\Support\Facades\Validator;

class SmsCode
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  \Closure $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if ($request->isMethod('post')) {
            Validator::make($request->all(), [
                'code' => 'required',
                'phone' => 'required'
            ])->validate();

            $code = Sms::getCode($request->get('phone'));
            if ($code == $request->get('code')) {
                return $next($request);
            }
            return back()->with('msg', '验证码不正确')->withInput();

        }
        return $next($request);
    }
}
